﻿/**
* File: XmglxmxxkdService.cs
* Author: 汪杰
* Create Time:2025/3/6 14:29:58
* Email:15200358008@139.com
* 代码由TMCodeGenerator自动生成
**/
using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JSLCloud.Attributes;
using JSLCloud.App.Models.Xmglxmxxkd;
using JSLCloud.DBModel;
using JSLCloud.Framework;
using JSLCloud.Models;
using JSLCloud.Utilities.SqlSugar;
using JSLCloud.Utilities.WebApi;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Data;


namespace JSLCloud.App.Mes.Service
{
    /// <summary>
    /// IXmglxmxxkdService 实现类
    /// </summary>
    [UseDI(ServiceLifetime.Scoped,typeof(ITpmpageconfigService))]
    public class TpmpageconfigService : ITpmpageconfigService
    {
        private readonly IDbContext _db;//数据库操作实例对象
        private readonly ILogger<TpmpageconfigService> _log;//日志操作实例对象
        private readonly IMapper _mapper;//AutoMapper实例
        
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="logger"></param>
        /// <param name="mapper"></param>
        public TpmpageconfigService(IDbContext dbContext, ILogger<TpmpageconfigService> logger,IMapper mapper)
        {
            _db = dbContext;
            _log = logger;
            _mapper = mapper;
        }
        
        /// <summary>
        /// 获取xm_gl_xmxxkd数据
        /// </summary>
        /// <returns></returns>
        public async Task<ResponseObject<List<string>>> GetTableListAsync()
        {
            try
            {
                var sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'jslproject' AND TABLE_NAME LIKE 'xm_%'";
                var table = await _db.Instance.Ado.GetDataTableAsync(sql);
                var result = new List<string>();
                foreach (DataRow row in table.Rows) 
                {
                    result.Add(row["table_name"].ToString());
                }

                //返回执行结果
                return ResponseUtil<List<string>>.SuccessResult(result);
            }
            catch (Exception ex)
            {
                //返回查询异常结果
                return ResponseUtil<List<string>>.FailResult(null, ex.Message);
            }
        }

        /// <summary>
        /// 获取xm_gl_xmxxkd数据
        /// </summary>
        /// <returns></returns>
        public async Task<ResponseObject<List<dynamic>>> GetTableColumnsAsync(string tableNmae)
        {
            try
            {
                var sql = "SELECT COLUMN_NAME AS columnname,is_nullable AS nullable,column_type AS columntype,column_comment AS columncomment " +
                    $"FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = 'jslproject' AND table_name = '{tableNmae}';";
                var table = await _db.Instance.Ado.GetDataTableAsync(sql);
                var result = new List<dynamic>();
                foreach (DataRow row in table.Rows)
                {
                    result.Add(new
                    {
                        columnname = row["columnname"].ToString(),
                        nullable = row["nullable"].ToString(),
                        columntype = row["columntype"].ToString(),
                        columncomment = row["columncomment"].ToString(),
                    });
                }

                //返回执行结果
                return ResponseUtil<List<dynamic>>.SuccessResult(result);
            }
            catch (Exception ex)
            {
                //返回查询异常结果
                return ResponseUtil<List<dynamic>>.FailResult(null, ex.Message);
            }
        }
    }
}
